import { Logger } from '../utils/logger';
export class CleanManager {
    static Current: CleanManager = new CleanManager();
    private svrList: ICleanService[] = [];
    private logger:Logger;

    constructor(){
        this.logger = new Logger();
    }

    RegistCleanService(svr: ICleanService) {
        if (!this.ServiceIsExist(svr.ServiceName)) {
            this.svrList.push(svr);
        }
    }

    async CleanData() {
        for (let item of this.svrList) {
            try {
                await item.CleanData();
            }
            catch (error) {
                this.logger.getLogger("error").error("清除订单表异常：", error);
            }
        }
        this.logger.getLogger("app").info("数据清理完成 ==================================================》");
        this.Dispose();
    }

    Dispose() {
        this.svrList = null;
        this.logger = null;
    }

    private ServiceIsExist(svrName: string) {
        let has = false;
        for (let item of this.svrList) {
            if (item.ServiceName == svrName) {
                has = true;
                break;
            }
        }

        return has;
    }
}

export interface ICleanService {
    ServiceName: string;
    CleanData;
}